package com.example.googleguavaapachecommons.googleguava.map.immutablemap;

import com.google.common.collect.ImmutableMap;

import java.util.Map;

/**
 * @author zwzheng
 * @Classname demo
 * @Description 已经确认范围的常量值key, value 可以用不可变集合ImmutableMap来存储
 * 不可变集合：在初始化数据后就不可修改
 * @Date 2021/1/4 15:33
 * @Version V1.0
 */
public class demo {
    //旧方法1
    public static String getGenderBySwitchCase(String type) {
        switch (type) {
            case "M":
                return "男性";
            case "F":
                return "女性";
            default:
                return null;
        }

    }

    //旧方法2
    public static String getGenderByIfElse(String type) {
        if ("M".equals(type)) {
            return "男性";
        } else if ("F".equals(type)) {
            return "女性";
        } else {
            return null;
        }
    }

    //新方法1
    public static Map<String, String> getGenderByImutableMap() {
        Map<String, String> genderMap = new ImmutableMap.Builder<String, String>()
                .put("M", "男性")
                .put("F", "女性")
                .build();

        return genderMap;

    }
    //新方法2
    public static Map getGenderByImmutableMap2(){
        Map<String, String> genderMap = ImmutableMap.of("M", "男性", "F", "女性");
        return genderMap;
    }


    public static void main(String[] args) {

        System.out.println("旧方法一：" + demo.getGenderBySwitchCase("M"));
        System.out.println("旧方法二：" + demo.getGenderByIfElse("M"));
        System.out.println("新方法一：" + demo.getGenderByImutableMap().get("M"));
        System.out.println("新方法二：" + demo.getGenderByImmutableMap2().get("M"));
    }


}